<template>
  <div class="EChart-div">
    <!-- 循环渲染每个图表的容器 -->
    <div v-for="(chartOption, index) in chartOptions" :key="index" :ref="el => { if (el) divs[index] = el }" style="width: 600px; height: 400px; margin-bottom: 20px;"></div>
  </div>
</template>

<script setup>
import * as echarts from 'echarts';
import { onMounted, ref, reactive } from 'vue';
// 存储所有图表容器的引用
const divs = reactive([]);

// 图表配置数组
const chartOptions = reactive([
  {
    title: {
      text: '图表 1'
    },
    tooltip: {},
    xAxis: {
      data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    },
    yAxis: {},
    series: [{
      name: '销量',
      type: 'bar',
      data: [5, 20, 36, 10, 10, 20]
    }]
  },
]);

onMounted(() => {
  // 初始化所有图表实例
  chartOptions.forEach((option, index) => {
    const myChart = echarts.init(divs[index]);
    myChart.setOption(option);
  });
});

</script>

<style scoped>

</style>
